package com.nian.global.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

/**
 * 基本功能工具
 *
 * Created by nian on 2023/4/16.
 */
public class BaseUtils {

    /**
     *
     * 构建当前时间
     *
     */
    public static String currentTime(String format) {
        return new SimpleDateFormat(format).format(System.currentTimeMillis());
    }

    /**
     *
     * 构建当前秒级时间戳
     *
     */
    public static String secondTimestamp() {
        return String.valueOf(Calendar.getInstance().getTimeInMillis() / 1000);
    }

    /**
     * 生成唯一键
     *
     */
    public static String uniqueKey(String start) {
        int random = new Random().nextInt(9000) + 10000;
        return start + Calendar.getInstance().getTimeInMillis() + random;
    }

    /**
     * 生成 beginIndex
     *
     */
    public static int beginIndex(int currentIndex, int limit) {

        return (currentIndex - 1) * limit;
    }

    /**
     * 生成 endIndex
     *
     */
    public static int endIndex(int currentIndex, int limit, int size) {

        return  (currentIndex * limit) > size ? size : (currentIndex * limit);
    }

    /**
     * 判断字符串中某个字符是大写的就转化为“/” 加小写
     *
     */
    public static String convertNameToPath(String name) {
        StringBuilder result = new StringBuilder();
        for (char c : name.toCharArray()) {
            if (Character.isUpperCase(c)) {
                result.append('/').append(Character.toLowerCase(c));
            } else {
                result.append(c);
            }
        }
        return result.toString();
    }
}
